package com.yyfax.pay.third.model.to.fuiou.xml;

import java.io.StringWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.QNameMap;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import com.thoughtworks.xstream.io.xml.StaxWriter;

/**
 * 支付接口请求参数封装类
 * 
 * @author heizq
 * @date 2017-04-11 20:03:59
 * @since v1.0.0
 */
@XStreamAlias("REQUEST")
public class PayReqData {

	/**
	 * 版本号
	 */
	@XStreamAlias("VERSION")
	private String version;

	/**
	 * 客户IP
	 */
	@XStreamAlias("USERIP")
	private String userip;

	/**
	 * 商户代码
	 */
	@XStreamAlias("MCHNTCD")
	private String mchntcd;

	/**
	 * 交易类型
	 */
	@XStreamAlias("TYPE")
	private String type;

	/**
	 * 商户订单号
	 */
	@XStreamAlias("MCHNTORDERID")
	private String mchntorderid;

	/**
	 * 用户编号
	 */
	@XStreamAlias("USERID")
	private String userid;

	/**
	 * 富友订单号
	 */
	@XStreamAlias("ORDERID")
	private String orderid;

	/**
	 * 银行卡号
	 */
	@XStreamAlias("BANKCARD")
	private String bankcard;

	/**
	 * 银行预留手机号
	 */
	@XStreamAlias("MOBILE")
	private String mobile;

	/**
	 * 短信验证码
	 */
	@XStreamAlias("VERCD")
	private String vercd;

	/**
	 * 保留字段1
	 */
	@XStreamAlias("REM1")
	private String rem1;

	/**
	 * 保留字段2
	 */
	@XStreamAlias("REM2")
	private String rem2;

	/**
	 * 保留字段3
	 */
	@XStreamAlias("REM3")
	private String rem3;

	/**
	 * 签名类型
	 */
	@XStreamAlias("SIGNTP")
	private String signtp;

	/**
	 * 摘要数据 TYPE+"|"+VERSION+"|"+MCHNTCD+"|"+ORDERID+"|"+MCHNTORDERID+"|"+USERID+"|"+BANKCARD+"|"+VERCD+"|"+MOBILE+"|"+ USERIP+"|"+"商户32位密钥key"
	 */
	@XStreamAlias("SIGN")
	private String sign;

	/**
	 * 下单验证码接口返回的SIGNPAY字段
	 */
	@XStreamAlias("SIGNPAY")
	private String signpay;

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getUserip() {
		return userip;
	}

	public void setUserip(String userip) {
		this.userip = userip;
	}

	public String getMchntcd() {
		return mchntcd;
	}

	public void setMchntcd(String mchntcd) {
		this.mchntcd = mchntcd;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMchntorderid() {
		return mchntorderid;
	}

	public void setMchntorderid(String mchntorderid) {
		this.mchntorderid = mchntorderid;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getBankcard() {
		return bankcard;
	}

	public void setBankcard(String bankcard) {
		this.bankcard = bankcard;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getVercd() {
		return vercd;
	}

	public void setVercd(String vercd) {
		this.vercd = vercd;
	}

	public String getRem1() {
		return rem1;
	}

	public void setRem1(String rem1) {
		this.rem1 = rem1;
	}

	public String getRem2() {
		return rem2;
	}

	public void setRem2(String rem2) {
		this.rem2 = rem2;
	}

	public String getRem3() {
		return rem3;
	}

	public void setRem3(String rem3) {
		this.rem3 = rem3;
	}

	public String getSigntp() {
		return signtp;
	}

	public void setSigntp(String signtp) {
		this.signtp = signtp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getSignpay() {
		return signpay;
	}

	public void setSignpay(String signpay) {
		this.signpay = signpay;
	}

	@Override
	public String toString() {
		return "PayReqData{" + "version='" + version + '\'' + ", userip='" + userip + '\'' + ", mchntcd='" + mchntcd + '\'' + ", type='" + type + '\''
				+ ", mchntorderid='" + mchntorderid + '\'' + ", userid='" + userid + '\'' + ", orderid='" + orderid + '\'' + ", bankcard='" + bankcard
				+ '\'' + ", mobile='" + mobile + '\'' + ", vercd='" + vercd + '\'' + ", rem1='" + rem1 + '\'' + ", rem2='" + rem2 + '\'' + ", rem3='"
				+ rem3 + '\'' + ", signtp='" + signtp + '\'' + ", sign='" + sign + '\'' + ", signpay='" + signpay + '\'' + '}';
	}

	/**
	 * 该实体转换成XML字符串
	 * 
	 * @author heizq
	 * @date 2017-4-12 10:52:52
	 * @since v1.0.0
	 * @return 返回XML字符串
	 */
	public String toXml() {
		QNameMap qmap = new QNameMap();
		// qmap.setDefaultNamespace("http://www.99bill.com/mas_cnp_merchant_interface");
		qmap.setDefaultPrefix("");
		StaxDriver staxDriver = new StaxDriver(qmap);
		XStream xstream = new XStream(staxDriver);
		xstream.autodetectAnnotations(true);
		xstream.ignoreUnknownElements();
		xstream.alias("REQUEST", OrderReqData.class);
		try {
			StringWriter strWriter = new StringWriter();
			// don't do startDocument and do repair namespaces
			StaxWriter sw = new StaxWriter(staxDriver.getQnameMap(), staxDriver.getOutputFactory().createXMLStreamWriter(strWriter), false, true);
			xstream.marshal(this, sw);
			sw.close();
			return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + strWriter.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * XML字符串转换成实体{@link PayReqData}
	 * 
	 * @author heizq
	 * @date 2017-4-12 10:53:28
	 * @since v1.0.0
	 * @param xml
	 * @return 返回{@link PayReqData}
	 */
	public static PayReqData fromXml(String xml) {
		QNameMap qmap = new QNameMap();
		qmap.setDefaultNamespace("http://www.99bill.com/mas_cnp_merchant_interface");
		qmap.setDefaultPrefix("");
		StaxDriver staxDriver = new StaxDriver(qmap);
		XStream xstream = new XStream(staxDriver);
		xstream.autodetectAnnotations(true);
		xstream.ignoreUnknownElements();
		xstream.alias("REQUEST", PayReqData.class);
		return (PayReqData) xstream.fromXML(xml);
	}
}
